programming4us
           
 
 
SQL Server

SQL Azure : Connecting to a SQL Azure Database (part 2) - Connecting from the Entity Framework

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/18/2010 9:22:55 AM

3.5.2. Connecting from the Entity Framework

This example uses the ADO.NET Entity Framework and lets it handle your connection. Follow these steps:

  1. Fire up Visual Studio 2010, and create a new Windows Forms Application project. For this simple example, you can use whatever project name you like.

  2. Right-click the project in the Solution Explorer window, and select Add → New Item from the context menu. Doing so brings up the Add New Item dialog.

  3. Select the ADO.NET Entity Data Model data template, as shown in Figure 2.

    Figure 2. Adding an ADO.NET entity data model
  4. Accept the default name of Model1 and click the Add button, which starts the Entity Data Model Wizard.

  5. The first page of wizard is the Choose Model Contents page, which lets you create a conceptual model of your database by reverse-engineering an existing database. Or, you can start with an empty model and build your conceptual model by hand first. For this example, select the Generate From Database option, and click Next.

    NOTE

    This section doesn't go deep into the ADO.NET Entity Framework. For more information about the Entity Framework, see Pro ADO.NET 4.0 Entity Framework by Scott Klein (Apress, 2010).

  6. Next is the Choose Your Data Connection page. You need to create a connection to your new EFAzure database, so click the New Connection button. Doing so brings up the Connection Properties dialog, which you've probably seen before; it lets you define a connection that your application uses to connect to the specified database.

  7. In the Connection Properties dialog, click the Change button to open the Choose Data Source dialog shown in Figure 3.

    Figure 3. Selecting the data source
  8. Select Microsoft SQL Server from the list of data sources, and click Continue to return to the Connection Properties dialog.

  9. Enter the server name and the administrator username and password, and select the EFAzure database (see Figure 4). The server name is the logical name of the server (FQDN); you can get that piece of information from the SQL Azure Portal in the Server Information section.

    Figure 4. The completed Connection Properties dialog
  10. To ensure that your connection works, click the Test Connection button. If you entered everything correctly and the firewall settings are correct, you get a message stating that the test connection succeeded.

  11. Click OK in the Connection Properties dialog. Doing so takes you back to the Entity Data Model Wizard's Choose Your Data Connection page.

  12. Your connection has been defined, and you're ready to proceed. You may need to select whether to include the sensitive data in the connection string or to exclude it. For the sake of this example, select the include option (see Figure 5). Keep the other default settings, and click Next.

    Figure 5. The completed Choose Your Data Connection wizard page
  13. The next step of the Entity Data Model Wizard allows you to select the objects from your database that you want to include in your conceptual model (see Figure 6). You see nodes for Views and Stored Procedures; but because you didn't create any, you can't select anything. Select the Contact table, and click Finish.

    Figure 6. Selecting database objects
  14. The Entity Data Model Wizard creates your conceptual model and displays it in the Visual Studio IDE. You won't work with this directly, so close it. You do need the form, so open it in Design view and place a ListBox on it, leaving the default name listbox1.

  15. Double-click the form (not the ListBox) to display the code-behind and create the form's Load event. You want to do something very simple: load the contacts from the Contact table into the list box when the form loads. (In normal circumstances, this wouldn't be a good idea; but because the table contains only a few contacts, and this example is demonstrating the functionality of querying an Azure database, you can let it slide.) In the Load event, enter the following code:

    using (EFAzureEntities context = new EFAzureEntities())
    {
    var query = from con in context.Contacts
    select con;

    foreach (var cont in query)
    {

    listBox1.Items.Add(cont.FirstName);
    }
    }

The first line creates an instance of the EFAzureEntities class. This class lets you work with database objects in terms of .NET object-oriented objects. You then use the Language Integrated Query (LINQ) language technology (in this case, LINQ to Entities) to query the Contact table and fill the list box with the first names of all the contacts, as shown in Figure 7.

Figure 7. Completed Form With Data From SQL Azure.

And there you have it; you've successfully queried the cloud. Yes, this is a simple example, but its purpose is to illustrate how easy it is to connect to SQL Azure, create and populate a database, and create an application that queries the database.

Other -----------------
- SQL Azure : Creating Databases, Logins, and Users (part 2)
- SQL Azure : Creating Databases, Logins, and Users (part 1)
- SQL Azure : Azure Server Administration (part 3) - Databases
- SQL Azure : Azure Server Administration (part 2) - Firewall Settings
- SQL Azure : Azure Server Administration (part 1) - Server Information
- SQL Azure : Managing Your Azure Projects
- SQL Azure : Creating Your Azure Account
- An OLAP Requirements Example: CompSales International (part 16) - Security and Roles
- An OLAP Requirements Example: CompSales International (part 15) - SSIS
- An OLAP Requirements Example: CompSales International (part 14) - Data Mining
- An OLAP Requirements Example: CompSales International (part 13) - Cube Perspectives
- An OLAP Requirements Example: CompSales International (part 12) - Generating a Relational Database
- An OLAP Requirements Example: CompSales International (part 11)
- An OLAP Requirements Example: CompSales International (part 10)
- An OLAP Requirements Example: CompSales International (part 9) - Browsing Data in the Cube
- An OLAP Requirements Example: CompSales International (part 8) - Aggregating Data Within the Cube
- An OLAP Requirements Example: CompSales International (part 7) - Building and Deploying the Cube
- An OLAP Requirements Example: CompSales International (part 6) - Creating the Cube
- An OLAP Requirements Example: CompSales International (part 5) - Creating the Other Dimensions
- An OLAP Requirements Example: CompSales International (part 4) - Defining Dimensions and Hierarchies
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us